<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label" style="width: 90px;">车牌号码：</span>
      <el-input v-model="params.carNumber" size="small" clearable placeholder="请输入内容" class="search-main"
        @clear="getList" />
      <span class="search-label" style="width: 90px;">车主姓名：</span>
      <el-input v-model="params.personName" size="small" clearable placeholder="请输入内容" class="search-main"
        @clear="getList" />
      <span class="search-label">状态：</span>
      <el-select size="small" style="width: 130px;" v-model="params.cardStatus" @change="getList">
        <!-- <el-option v-for="item in []" :key="item.id" /> -->
        <el-option label="全部" :value="null"></el-option>
        <el-option label="可用" :value="0"></el-option>
        <el-option label="已过期" :value="1"></el-option>
      </el-select>
      <el-button type="primary" class="search-btn" size="small" @click="getList">查询</el-button>
    </div>
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <div style="padding-right: 300px;">
        <el-button type="primary" size="small" @click="addCard">添加月卡</el-button>
        <el-button @click="delMore" size="small">批量删除</el-button>
      </div>
      <div>
        <el-tag style="text-align: right;">本园区共计 {{areaToatl}} 个车位，月卡用户 {{ total }} 人，车位占有率 {{spaceProportion}}%</el-tag>
      </div>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="list" style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column type="index" label="序号" />
        <el-table-column label="车主名称" prop="personName" />
        <el-table-column label="联系方式" prop="phoneNumber" />
        <el-table-column label="车牌号码" prop="carNumber" />
        <el-table-column label="车辆品牌" prop="carBrand" />
        <el-table-column label="剩余有效天数" prop="totalEffectiveDate" />
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="scope">
            <!-- <template> -->
            <el-button size="mini" type="text" @click="jump(scope.row.id, '月卡续费')">续费</el-button>
            <el-button size="mini" type="text" @click="jump(scope.row.id, '查看详情')">查看</el-button>
            <el-button size="mini" type="text" @click="jump(scope.row.id, '编辑月卡')">编辑</el-button>
            <el-button size="mini" type="text" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页结构 -->

    <div class="page-container">
      <el-pagination layout="total, sizes, prev, pager, next" :total="total" :current-page="params.page"
        :page-sizes="[2, 4, 6, 8, 10]" :page-size="params.pageSize" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </div>
    <!-- 添加楼宇 -->
    <el-dialog title="添加楼宇" width="580px">
      <!-- 表单接口 -->
      <div class="form-container">
      </div>
      <template #footer>
        <el-button size="mini">取 消</el-button>
        <el-button size="mini" type="primary">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>


<script>
import { getMonthCarListAPI, getCarListAPI, delCarCardAPI } from '@/api/car'
export default {
  data() {
    return {
      areaToatl: '',
      spaceProportion:'',
      idstr: '',
      list: [],
      total: 0,
      params: {
        page: 1,
        pageSize: 10,
        carNumber: null,
        personName: null,
        cardStatus: null
      }
    }
  },
  methods: {
    handleSelectionChange(val) {
      // console.log(val)
      let str = ''
      for (let index = 0; index < val.length; index++) {
        const element = val[index];
        str = str + element.id + ','
      }
      // 去掉最后一个字符
      this.idstr = str.slice(0, -1)
    },
    delMore() {
      this.$confirm('你确定要删除吗?').then(() => {
        delCarCardAPI(this.idstr).then(() => {
          this.$message.success('删除成功!')
          this.getList()
        })
      })
    },
    jump(id, name) {
      // console.log(name)
      // console.log(id)
      if (name === '查看详情')
        this.$router.push('/showDetail?id=' + id)
      else
        this.$router.push('/addCard?id=' + id + '&name=' + name)
    },
    getList() {
      getMonthCarListAPI(this.params).then((res) => {
        // console.log(res.data.rows),
        this.list = res.data.rows,
          this.total = res.data.total
      })
    },

    handleSizeChange(pageSize) {
      this.params.page = 1,
        // console.log(`每页 ${val} 条`);
        this.params.pageSize = pageSize,
        this.getList()
    },
    handleCurrentChange(page) {
      // console.log(`当前页: ${val}`);
      this.params.page = page
      this.getList()
    },
    addCard() {
      this.$router.push('/addCard')
    },
    del(id) {
      this.$confirm('你确定要删除吗???').then(() => {
        delCarCardAPI(id).then(() => {
          this.getList()
          this.$message.success('删除成功！')
        }).catch(() => {
          this.$message.error('删除失败！')
        })
      }).catch(() => {
        this.$message.error('已取消！')
      })
    }
  },
  // 生命周期函数，页面加载后触发
  mounted() {
    getCarListAPI().then((res) => {
      this.areaToatl = res.data.totalSpaceNum
this.spaceProportion=res.data.spaceProportion
      // console.log(res.data)
    })
    this.getList()
  }

}
</script>


<style lang="scss" scoped>
.card-container {
  padding: 10px;
  background-color: #fff;
}


.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-main {
    width: 220px;
    margin-right: 10px;
  }

  .search-btn {
    margin-left: 20px;
  }
}

.create-container {
  display: flex;
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}
</style>